var startYear;
var endYear; 
var type;
var voltage;
$("#tab3").click(function(){
	var width=parseInt($("#pane3").css("width"))-280;
	$("#yearCountLine").css("width",width);
	$("#fault_line").css("width",width);
	$("#faultType_pie").css("width",width);
	$("#fault_pie").css("width",width);
	$("#fault_multi_bar").css("width",width);
	$("#companyYearLine").css("width",width);
	yearCountLineChart.getChart().resize();
	lineChart.getChart().resize();
	faultType_pie.getChart().resize();
	pieChart.getChart().resize();
	barChart.getChart().resize();
	companyYearLineChart.getChart().resize();
});
   
 function getDatas(){
	type=$("#chooseType").val();
	startYear=$('#startYear').val();
	endYear=$('#endYear').val();
	voltage=$('#voltage').val();
	if(startYear==endYear){
		$("#faultTypeTbDiv").hide();
		faultTypeTableShow = false;
		$("#companyYear").hide();
		$("#yearCount").hide();
		$.getJSON('/rest/transmissionline/getLineFaultByYearAndType/'+startYear+'/'+type+"/"+voltage, function(result) {
			getYearChartDatas(result);
		});
	}else if(Number(startYear)<Number(endYear)){
		$("#faultTypeTbDiv").hide();
		faultTypeTableShow = false;
		$("#companyYear").show();
		$("#yearCount").show();
		$.getJSON('/rest/transmissionline/getLineFaultByYearIntervalAndType/'+startYear+'/'+endYear+'/'+type+"/"+voltage, function(result) {
			getChartDatas(result);
		});
	}
   }
   var yearCountLineChart=new CategoryLine("yearCountLine");
   var lineChart=new CategoryLine("fault_line");
   var pieChart=new Echart3NormalPie("fault_pie");
   var barChart=new CategoryBar("fault_multi_bar");
   var faultType_pie=new Pie("faultType_pie");
   var companyYearLineChart=new CategoryLine("companyYearLine");
   // 查询按钮
   $("#check-btn").on('click',function(){
	   getDatas();
   });
   // 一年
   function getYearChartDatas(result){
	   pieChart=new Pie("fault_pie");
	   barChart=new CategoryBar("fault_multi_bar");
	   lineChart=new CategoryLine("fault_line");
	   // 故障类型图
	   initFaultTypePie(result.faultTypeCount);
	   // 第一个月度图
	   var xLineValues=[];
	   for(var i=1;i<13;i++){
		   xLineValues.push(i+"月");
	   }
	   lineChart.setDataZoom(true).setxAxis(xLineValues).setyAxis(result.months);
	   var lineOpt=lineChart.getOption();
	   lineOpt.series[0].label.normal.position="top";
	   lineChart.setxAxisTitle("月份");
	   lineChart.setyAxisTitle("次数");
	   lineChart.setyAxisName('次数');
	   lineChart.show();
	   // 调管单位图
	   var companyMonthdata = {
   			name:'',
   			xData:{
   				name:'月份',
   				value:['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
   			},
   			yData:{
   				name:'故障次数',
   				value:[]
   			}
   	  };
	   
	  // 单位柱形图
	   for(var rankName in result.companyMonthCount){
		   var obj2={
				   lineName:'',
				   lineValue:[]
		   };
		   obj2.lineName=rankName;
		   obj2.lineValue=result.companyMonthCount[rankName];
		   companyMonthdata.yData.value.push(obj2);
	   }
	   var barOpt=barChart.getOption();
	   barOpt.tooltip.axisPointer.type="shadow";
	   barOpt.grid.top=70;
	   barChart.setMagicType(false).setZoomTool(false).setRestore(false);
	   barChart.setDataZoom(true).setDataZoomStartValue(0,2).setData(companyMonthdata).show();
	   
	   // 单位饼图
	    var pieOpt=pieChart.getOption();
	    pieOpt.legend.show=false;
		pieChart.setPie(result.companyTotalCount).setPieOuterRadius('50%').setPieLabelFormatter("{b}: {c} ({d}%)");
		pieChart.show();
		
        // 添加标题
		showTitle(result.total);
   }
   // 多年
   function  getChartDatas(result){
	   pieChart=new Echart3NormalPie("fault_pie");
	   barChart=new CategoryBar("fault_multi_bar");
	   initYearCountLineChart(result.yearCounts);
	   initMonthBar(result);
	   initFaultTypePie(result.faultTypeCount);
	   var companyData=[];
	   var companyBardata = {
			   name:'',
			   xData:{
				   name:'单位',
				   value:[]
			   },
			   yData:{
				   name:'次数',
				   value:[{lineName:'',lineValue:[]}]
			   }
	   };
	   for(var i=0;i<result["ranks"].length;i++){
		   var obj=result["ranks"][i];
		   var obj1={
				   value:'',
				   name:''
		   };
		   companyBardata.xData.value.push(obj[0]);
		   companyBardata.yData.value[0].lineValue.push(obj[1]);
		   obj1.value=obj[1];
		   obj1.name=obj[0];
		   companyData.push(obj1);
	   }
	   pieChart.setDataNameValues(companyData);
	   pieChart.show();
	   
	   var barOpt=barChart.getOption();
	   barOpt.tooltip.axisPointer.type="shadow";
	   barOpt.grid.top=70;
	   barChart.setMagicType(false).setZoomTool(false).setRestore(false);
	   barChart.setDataZoom(true).setData(companyBardata).setyAxisName("故障次数").show();
	   /*barChart.getChart().on('click',function(params){
		   var companyName=params.name;
		   initCompanyLineChart(result.companyYearCounts[companyName],companyName);
	   })*/
	   barChart.getChart().getZr().on('click',function(params){
         var pointInPixel= [params.offsetX, params.offsetY];
         if (barChart.getChart().containPixel('grid',pointInPixel)) {
             var xIndex=barChart.getChart().convertFromPixel({seriesIndex:0},[params.offsetX, params.offsetY])[0];
             var companyName=result["ranks"][xIndex][0];
  		     initCompanyLineChart(result.companyYearCounts[companyName],companyName);
         }
	   });
	   initCompanyLineChart(result.companyYearCounts[result["ranks"][0][0]],result["ranks"][0][0]);
	   // 添加标题
	   showTitle(result.total);
   }
   //年度总计折线图
   function initYearCountLineChart(dataObj){
	   $("#yearCountLineTitle").html(startYear+"年-"+endYear+"年输电线路"+type+"故障年度统计");
	   yearCountLineChart =new CategoryLine("yearCountLine");
	   var xAxisData=[];
	   var yAxisData=[];
	   for(var i=startYear;i<=endYear;i++){
		   xAxisData.push(i);
		   yAxisData.push(dataObj[i]);
	   }
	   yearCountLineChart.setxAxis(xAxisData);
	   yearCountLineChart.setyAxis(yAxisData);
	   yearCountLineChart.setxAxisTitle("年份");
	   yearCountLineChart.setyAxisTitle("故障次数");
	   yearCountLineChart.setyAxisName("故障次数");
	   yearCountLineChart.show();
   }
   // 各单位历史趋势图
   function initCompanyLineChart(dataArr,companyName){
	   $("#companyYearTitle").html(startYear+"年-"+endYear+"年"+companyName+type+"故障次数统计");
	   companyYearLineChart =new CategoryLine("companyYearLine");
	   var xAxisData=[];
	   for(var i=startYear;i<=endYear;i++){
		   xAxisData.push(i);
	   }
	   companyYearLineChart.setxAxis(xAxisData);
	   companyYearLineChart.setyAxis(dataArr);
	   companyYearLineChart.setxAxisTitle("年份");
	   companyYearLineChart.setyAxisTitle("故障次数");
	   companyYearLineChart.setyAxisName("故障次数");
	   companyYearLineChart.show();
   }
   
   // 第一个月度柱状图
   function initMonthBar(result){
	   lineChart=new CategoryBar("fault_line");
	   var monthData =result.months;
	   var xLineValues=[];
	   for(var year in monthData){
		   lineChart.setyAxis(monthData[year]).setyAxisName(year+"年");
	   }
	   for(var i=1;i<13;i++){
		   xLineValues.push(i+"月");
	   }
//	   lineChart.setDataZoom(true).setxAxis(xLineValues);
	   lineChart.setxAxis(xLineValues);
	   var lineOpt=lineChart.getOption();
	   lineOpt.series[0].label.normal.position="top";
	   lineChart.setxAxisTitle("11月份");
	   lineChart.setyAxisTitle("次数");
	   lineChart.show();
   }
   // 第二个故障类型饼图
   var faultTypeTable=null;
   var faultTypeTableShow = false;
   var initfaultTypeTable = false;
   var selectedFaultType;
   function initFaultTypePie(dataMap){
	   faultType_pie=new Pie("faultType_pie");
	   var dataArr=[];
	   for(var type1 in dataMap){
		   dataArr.push([type1,dataMap[type1]]);
	   }
	   var option = faultType_pie.getOption();
	   option.legend.orient="vertical";
	   faultType_pie.setLegendPosition("left","top").setPie(dataArr).setPieOuterRadius('75%').setPieLabelFormatter("{b}: {c} ({d}%)");
	   option.series[0].label.normal.textStyle.fontSize='16';
	   option.series[0].label.emphasis.textStyle.fontSize='18';
	   option.series[0].startAngle='0';
	   faultType_pie.show();
	   faultType_pie.getChart().on("click",function(params){
		   if(faultTypeTableShow && selectedFaultType==params.name){
			   $("#faultTypeTbDiv").hide();
			   faultTypeTableShow = false;
		   }else{
			   selectedFaultType=params.name;
			   $("#faultTypeTbDiv").show();
			   faultTypeTableShow = true;
			   $.getJSON('/rest/transmissionline/getLineFaultDetail/'+startYear+'/'+endYear+'/'+type+"/"+voltage+"/"+selectedFaultType, function(result) {
				   initFaultTables(result);
				});
		   }
	   });
   }
   function getDataset(faults) {
		var dataset = [];
		for ( var i in faults) {
			var f = faults[i];
			var faultTime = new Date(f.faultTime.time ? f.faultTime.time
					: f.faultTime);
			dataset.push([faultTime.toLocaleString(), getStr(f.equipment), 
					getStr(f.faultType), getStr(f.faultReson),
					getStr(f.faultPhase), getStr(f.rank),getStr(f.remark) ]);
		}
		return dataset;
	}
   function getStr(text) {
		return text ? text : '';
	}
   function initFaultTables(data) {
	   if(initfaultTypeTable){
		   faultTypeTable.reload(getDataset(data));
	   } else {
		   var option = MyTable.getDefaultOptions();
		   option.paging = true;
		   option.pageLength = 10;
		   option.searching = true;
		   var columns = [ '故障时间', '线路', '故障类型', '故障原因', '故障相别', '调管单位',"备注" ];
		   faultTypeTable = new MyTable('faultTypeTable', columns, option);
		   initfaultTypeTable = true;
		   faultTypeTable.setDataSet(getDataset(data)).show();
	   }
	}
   function showTitle(total){
	   if(startYear==endYear){
			var year=startYear+"年";
		}else{
			var year=startYear+"年-"+endYear+"年";
		}
	    $("#total").html(year+"输电线路"+type+"故障总数："+total);
		if($("#lineTitle").empty()){
			$("#lineTitle").append(year+"输电线路"+type+"故障月度统计");
		}else{
			$("#lineTitle").empty();
		}
		$("#faultTypeTitle").html(year+"输电线路"+type+"故障类型统计");
		if($("#barTitle").empty()){
			$("#barTitle").append(year+"运维单位线路"+type+"故障统计");
		}else{
			$("#barTitle").empty();
		}
		if($("#pieTitle").empty()){
			$("#pieTitle").append(year+"各运维单位线路"+type+"故障统计占比分析");
		}else{
			$("#pieTitle").empty();
		}
   }